<!--
@license
Copyright 2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div
  #main
  [class]="'main ' + mode + ' ' + timeProperty"
  detectResize
  (onResize)="onResize()"
  observeIntersection
  (onVisibilityChange)="onVisibilityChange($event)"
>
  <svg class="axis x-axis">
    <g #xAxis></g>
    <g class="tooltip">
      <!-- d3 places axis label at 9px below from the top edge. -->
      <g
        *ngIf="tooltipData"
        [style.transform]="getCssTranslatePx(tooltipData.xAxis.position, 9)"
      >
        <text>{{tooltipData.xAxis.label}}</text>
      </g>
    </g>
  </svg>
  <div class="axis y-axis">
    <svg>
      <g #yAxis></g>
      <!-- d3 places axis label at 9px right from the left edge. -->
      <g class="tooltip" [style.transform]="getCssTranslatePx(9, 0)">
        <g
          *ngIf="tooltipData"
          [style.transform]="getGroupTransform(tooltipData.closestDatum)"
        >
          <text [attr.y]="tooltipData.yAxis.position">
            {{tooltipData.yAxis.label}}
          </text>
        </g>
      </g>
    </svg>
    <!-- Disable the feature when in non-offset and non-step mode. -->
    <ng-container *ngIf="isTimeSelectionEnabled(timeSelection)">
      <histogram-card-fob-controller
        class="histogram-card-fob"
        [timeSelection]="timeSelection"
        [steps]="getSteps()"
        [temporalScale]="scales.temporalScale"
        (onTimeSelectionChanged)="onLinkedTimeSelectionChanged.emit($event)"
        (onTimeSelectionToggled)="onLinkedTimeToggled.emit()"
      ></histogram-card-fob-controller>
    </ng-container>
  </div>
  <svg #content class="content">
    <g class="grid">
      <g
        *ngFor="let tick of getGridTickYLocs()"
        [style.transform]="getCssTranslatePx(0, tick)"
      >
        <line class="tick" x2="100%"></line>
      </g>
    </g>

    <g
      #histograms
      [class.histograms]="true"
      [class.linked-time-single-step]="timeSelection && !timeSelection.end"
    >
      <g
        *ngFor="let datum of data; trackBy: trackByWallTime;"
        [style.transform]="getGroupTransform(datum)"
        [class.histogram]="true"
        [class.no-color]="!isDatumInTimeSelectionRange(datum)"
        [style.color]="getHistogramFill(datum)"
        (mouseenter)="updateColorOnHover($event, datum, true)"
        (mouseleave)="updateColorOnHover($event, datum, false)"
        (click)="onLinkedTimeRangeChanged(datum)"
      >
        <line
          *ngIf="mode === HistogramMode.OFFSET"
          class="baseline"
          x2="100%"
        ></line>
        <path [attr.d]="getHistogramPath(datum)"></path>
        <circle
          *ngIf="tooltipData"
          r="2"
          [style.transform]="getCssTranslatePx(
          getUiCoordFromBinForContent(
            getClosestBinFromBinCoordinate(
              datum,
              tooltipData.xPositionInBinCoord
            )
          ).x,
          getUiCoordFromBinForContent(
            getClosestBinFromBinCoordinate(
              datum,
              tooltipData.xPositionInBinCoord
            )
          ).y
        )"
        ></circle>
      </g>
    </g>

    <g class="tooltip" *ngIf="tooltipData">
      <g [style.transform]="getGroupTransform(tooltipData.closestDatum)">
        <path [attr.d]="getHistogramPath(tooltipData.closestDatum)"></path>
        <circle
          *ngIf="tooltipData.closestBin"
          r="2"
          [attr.cx]="getUiCoordFromBinForContent(tooltipData.closestBin).x"
          [attr.cy]="getUiCoordFromBinForContent(tooltipData.closestBin).y"
        ></circle>
      </g>
      <g
        class="value-label"
        [style.transform]="
          getCssTranslatePx(
            tooltipData.value.position.x,
            tooltipData.value.position.y
          )
        "
      >
        <!-- 3px margin is to make the label show nicely beside a cursor. -->
        <text x="3" y="-3">{{tooltipData.value.label}}</text>
      </g>
    </g>
  </svg>
</div>
